package com.hj.saapp.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hj.saapp.R;

import java.lang.reflect.Field;


/**
 * 类说明：默认弹出框
 *
 * @author YZ
 * @date Dec 17, 2014
 * @version 1.0
 */

@SuppressLint("ValidFragment")
public class DefaultingDialog extends DialogFragment implements View.OnClickListener
{
    
    private TextView titleTv;
    
    private TextView contentTv;
    
    private TextView positiveTv;
    
    private TextView negativeTv;
    

    private OnDialogClickListener onDialogClickListener;

    private String title;

    private String content;

    private String positiveStr;
    
    public String negativeStr;

    private View dv;

    private boolean isCancelable = false;

    public DefaultingDialog(Context context, int theme, String title, String content, String positiveStr,
                            String negativeStr, boolean isCancelable)
    {
        this.title = title;
        this.content = content;
        this.positiveStr = positiveStr;
        this.negativeStr = negativeStr;
        this.isCancelable = isCancelable;
    }
    
    @Override
    public void onStart()
    {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels - 30, getDialog().getWindow().getAttributes().height);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View v = inflater.inflate(R.layout.view_defaulting_dialog, container, false);
        
        titleTv = (TextView)v.findViewById(R.id.view_defaulting_dialog_titleTv);
        contentTv = (TextView)v.findViewById(R.id.view_defaulting_dialog_contentTv);
        positiveTv = (TextView)v.findViewById(R.id.view_defaulting_dialog_positiveTv);
        negativeTv = (TextView)v.findViewById(R.id.view_defaulting_dialog_negativeTv);
        dv = v.findViewById(R.id.dv);

        positiveTv.setOnClickListener(this);
        negativeTv.setOnClickListener(this);

        if (content == null)
        {
            dv.setVisibility(View.GONE);
        }

        setTitle(title);
        setContent(content);
        setPositive(positiveStr);
        setNegative(negativeStr);
        if (isCancelable)
        {
            this.setCancelable(true);
            LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.root);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DefaultingDialog.this.dismissAllowingStateLoss();
                }
            });
        }

        
        return v;
    }
    
    private void setTitle(String title)
    {
        if (title != null)
        {
            titleTv.setText(Html.fromHtml(title));
            titleTv.setVisibility(View.VISIBLE);
        }
        else
        {
            titleTv.setVisibility(View.GONE);
        }
        
    }
    
    private void setContent(String content)
    {
        if (content != null)
        {
            contentTv.setText(Html.fromHtml(content));
            contentTv.setVisibility(View.VISIBLE);
        }
        else
        {
            contentTv.setVisibility(View.GONE);
        }
    }
    
    private void setPositive(String positive)
    {
        if (positive != null)
        {
            positiveTv.setText(positive);
        }
        else
        {
            positiveTv.setVisibility(View.GONE);
        }
    }
    
    private void setNegative(String negative)
    {
        if (negative != null)
        {
            if (this.positiveStr == null)
            {
                negativeTv.setBackgroundResource(R.drawable.circle_corner_square_btn_big_life);
            }
            negativeTv.setText(negative);
        }
        else
        {
            negativeTv.setVisibility(View.GONE);
        }
    }
    
    @Override
    public void onClick(View v)
    {
        int viewID = v.getId();
        if (viewID == positiveTv.getId())
        {
            if (onDialogClickListener != null)
            {
                onDialogClickListener.onPositiveClickListener();
            }
        }
        else if (viewID == negativeTv.getId())
        {
            if (onDialogClickListener != null)
            {
                onDialogClickListener.onNegativeClickListener();
            }
        }
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        dismissAllowingStateLoss();
    }
    
    public static interface OnDialogClickListener
    {
        public void onPositiveClickListener();
        
        public void onNegativeClickListener();
    }
    
    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener)
    {
        this.onDialogClickListener = onDialogClickListener;
    }
    @Override
    public void onDetach() {

        super.onDetach();



        try {

            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");

            childFragmentManager.setAccessible(true);

            childFragmentManager.set(this, null);



        } catch (NoSuchFieldException e) {

            throw new RuntimeException(e);

        } catch (IllegalAccessException e) {

            throw new RuntimeException(e);

        }

    }


}
